package com.bfsuol.yihang.cars.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bfsuol.yihang.cars.api.QueryVehicleService;
import com.bfsuol.yihang.cars.domain.VehicleService;
import com.bfsuol.yihang.cars.dto.VehicleDTO;
import com.bfsuol.yihang.cars.entity.VehicleEntity;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class QueryVehicleServiceImpl implements QueryVehicleService {

    @Autowired
    private VehicleService vehicleService;

    @Override
    public Page<VehicleDTO> page(Page page1, VehicleDTO vehicleDTOParam) {

        QueryWrapper queryWrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(vehicleDTOParam.getVehicleNo())){
            queryWrapper.like("vehicle_no",  vehicleDTOParam.getVehicleNo());
        }
        if(vehicleDTOParam.getCustomerId()!=null){
            queryWrapper.eq("customer_id", vehicleDTOParam.getCustomerId());
        }
        if(!StringUtils.isEmpty(vehicleDTOParam.getName())){
            queryWrapper.like("name", vehicleDTOParam.getName());
        }
        if(!StringUtils.isEmpty(vehicleDTOParam.getCustomerName())){
            queryWrapper.like("customer_name",vehicleDTOParam.getCustomerName());
        }
        if(vehicleDTOParam.getIsSearchExpireDaysCar()){
            queryWrapper.le("touBao_remaining_days", 60);
        }
        Page<VehicleEntity> pages = vehicleService.getBaseMapper().selectPage(page1,queryWrapper);
        List<VehicleDTO> list = Lists.newArrayList();
        pages.getRecords().forEach(vehicleEntity -> {
            VehicleDTO vehicleDTO = new VehicleDTO();
            BeanUtils.copyProperties(vehicleEntity, vehicleDTO);
            list.add(vehicleDTO);
        });
        Page<VehicleDTO> pages1 = new Page<>();
        BeanUtils.copyProperties(pages,pages1);
        pages1.setRecords(list);
        return page1;
    }

    @Override
    public VehicleDTO getById(Long id) {
        VehicleDTO vehicleDTO = new VehicleDTO();
        BeanUtils.copyProperties(vehicleService.getById(id), vehicleDTO);
        return vehicleDTO;
    }

    @Override
    public VehicleDTO getOneByVehicleNo(String vehicleNo) {
        return vehicleService.getOneByVehicleNo(vehicleNo);
    }

    @Override
    public List<VehicleDTO> findAll() {
        List<VehicleEntity> vehicleEntities = vehicleService.list();
        return vehicleEntities.stream().map(vehicleEntity -> {
            VehicleDTO vehicleDTO = new VehicleDTO();
            BeanUtils.copyProperties(vehicleEntity, vehicleDTO);
            return vehicleDTO;
        }).collect(Collectors.toList());
    }
}
